package cn.meizhu.util;

import cn.meizhu.org.mapper.EmployeeMapper;
import cn.meizhu.user.domain.LoginInfo;
import cn.meizhu.user.mapper.LoginInfoMapper;
import cn.meizhu.user.mapper.UserMapper;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServletRequest;


@Component
public class LoginContext {

    public static Object loginContextUser(HttpServletRequest request){
        //获取请求头
        String token = request.getHeader("U-TOKEN");
        WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
        RedisTemplate redisTemplate = webApplicationContext.getBean("redisTemplate", RedisTemplate.class);

        LoginInfo loginInfo = (LoginInfo) redisTemplate.opsForValue().get(token);
        if (loginInfo.getType()==0){
            EmployeeMapper employeeMapper = webApplicationContext.getBean("employeeMapper", EmployeeMapper.class);
            return employeeMapper.findByEmployee(loginInfo.getId());
        }else {
            UserMapper userMapper = webApplicationContext.getBean("userMapper", UserMapper.class);
            return userMapper.findByUser(loginInfo.getId());
        }
    }
}
